﻿using System;
using System.Collections.Generic;
using System.Web;
using BangleCMS.Model;
using BangleCMS.BLL;

/// <summary>
///WebPageBase 的摘要说明
/// </summary>
public class WebPageBase : System.Web.UI.Page
{
    public WebPageBase()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    #region 网站属性
    private WebConfig _webside;
    public WebConfig WebSideConfig
    {
        get
        {
            return _webside;
        }
        set
        {
            _webside = value;
        }
    }

    private string GetConfigValue(string ConfigKey)
    {
        try
        {
            web_config model = new web_config();
            web_configBLL bll = new web_configBLL();
            model = bll.GetModel(ConfigKey);

            if (model == null)
            {
                return "";
            }
            else
            {
                return model.ConfigValue;
            }
        }
        catch (Exception ex)
        {
            return "";
        }
    }
    #endregion

    /// <summary>
    /// 重写加载事件
    /// </summary>
    /// <param name="e"></param>
    protected override void OnLoad(EventArgs e)
    {
        if (WebSideConfig == null)
        {
            WebSideConfig = new WebConfig();
            //设置网站属性
            WebSideConfig.WebSideName = GetConfigValue("WebSideName");
            WebSideConfig.Keywords = GetConfigValue("Keywords");
            WebSideConfig.Description = GetConfigValue("Description");
            WebSideConfig.Fooder1 = GetConfigValue("Fooder1");
            WebSideConfig.Fooder2 = GetConfigValue("Fooder2");
        }

        base.OnLoad(e);
    }

    #region URL参数解析
    /// <summary>
    /// URL参数解析
    /// </summary>
    /// <param name="name">参数名称</param>
    /// <returns>值(INT型)</returns>
    protected int ParseQueryInt(string name)
    {
        int i = 0;
        if (Request.QueryString[name] != null && Request.QueryString[name].ToString().Length < 0)
        {
            int.TryParse(Request.QueryString[name], out i);
        }
        return i;
    }

    /// <summary>
    /// URL参数解析
    /// </summary>
    /// <param name="name">参数名称</param>
    /// <param name="num">默认值</param>
    /// <returns>值(INT型)</returns>
    protected int ParseQueryInt(string name, int num)
    {
        int i = num;
        if (Request.QueryString[name] != null && Request.QueryString[name].ToString().Length>0)
        {
            int.TryParse(Request.QueryString[name], out i);
        }
        return i;
    }

    /// <summary>
    /// URL参数解析
    /// </summary>
    /// <param name="name">参数名称</param>
    /// <returns>值(STRING型)</returns>
    protected string ParseQueryString(string name)
    {
        string s;

        if (Request.QueryString[name] == null)
        {
            throw new ArgumentException("Can not find parameter:'" + name + "'");
        }

        s = Request.QueryString[name];
        return s;
    }

    /// <summary>
    /// URL参数解析
    /// </summary>
    /// <param name="name">参数名称</param>
    /// <param name="str">默认值</param>
    /// <returns>值(STRING型)</returns>
    protected string ParseQueryString(string name, string str)
    {
        string s;

        if (Request.QueryString[name] == null)
        {
            //throw new ArgumentException("Can not find parameter:'" + name + "'");
            s = str;
        }

        s = Request.QueryString[name];
        return s;
    }

    /// <summary>
    /// URL参数解析
    /// </summary>
    /// <param name="name">参数名称</param>
    /// <returns>值(INT型数组)</returns>
    protected int[] ParseQueryStringIntArray(string name)
    {
        if (Request.QueryString[name] == null)
        {
            throw new ArgumentException("Can not find parameter:'" + name + "'");
        }
        string[] strArr = Request.QueryString.GetValues(name);
        int[] iArr = new int[strArr.Length];
        try
        {
            for (int loop = 0; loop < strArr.Length; loop++)
            {
                iArr[loop] = int.Parse(strArr[loop]);
            }
        }
        catch
        {
            throw new ArgumentException("parameter:'" + name + "' value error!");
        }
        return iArr;
    }

    /// <summary>
    /// URL参数解析
    /// </summary>
    /// <param name="name">参数名称</param>
    /// <returns>值(STRING型数组)</returns>
    protected string[] ParseQueryStringStringArray(string name)
    {
        if (Request.QueryString[name] == null)
        {
            throw new ArgumentException("Can not find parameter:'" + name + "'");
        }
        string[] strArr = Request.QueryString.GetValues(name);
        return strArr;
    }
    #endregion

    #region JS方法
    /// <summary>
    /// 消息提示框
    /// </summary>
    /// <param name="Msg">提示内容</param>
    public void Alert(string Msg)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "系统提示", "alert('" + Msg + "')", true);
    }

    /// <summary>
    /// 消息提示框
    /// </summary>
    /// <param name="Title">标题</param>
    /// <param name="Msg">提示内容</param>
    public void Alert(string Title, string Msg)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), Title, "alert('" + Msg + "')", true);
    }

    /// <summary>
    /// 消息提示并刷新页面
    /// </summary>
    /// <param name="Msg"></param>
    /// <param name="url"></param>
    public void Redirect(string Msg)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "系统提示", "alert('" + Msg + "');window.location.href=window.location.href=", true);
    }

    /// <summary>
    /// 消息提示并重定向新的url
    /// </summary>
    /// <param name="Msg">提示内容</param>
    /// <param name="url">url</param>
    public void Redirect(string Msg, string Url)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "系统提示", "alert('" + Msg + "');window.location.href='" + Url + "'", true);
    }

    /// <summary>
    /// 消息提示并重定向新的url
    /// </summary>
    /// <param name="Title">标题</param>
    /// <param name="Msg">提示内容</param>
    /// <param name="url">url</param>
    public void Redirect(string Title, string Msg, string Url)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "系统提示", "alert('" + Msg + "');window.location.href='" + Url + "'", true);
    }
    #endregion

    #region 文本工具类（用于处理一些文本的转换等）
    /// <summary>
    /// 用于防止简单的查询时的SQL注入
    /// </summary>
    /// <param name="oldStr">原始文本</param>
    /// <returns>替换后的文本</returns>
    public static string ReplaceText(string oldStr)
    {
        //需要被替换的字符
        string[] str_illegal = new string[] { "'" };
        //替换后的字符(索引对应)
        string[] str_legitimate = new string[] { "" };

        string newStr = "";

        for (int i = 0; i < str_illegal.Length; i++)
        {
            newStr = oldStr.Replace(str_illegal[i], str_legitimate[i]);
        }

        return newStr;
    }
    #endregion

    #region 统一时间格式化

    /// <summary>
    /// 统一时间格式化
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public string SetShortDateTime(string str)
    {
        string reString = "";
        try
        {
            DateTime date = Convert.ToDateTime(str);
            reString = date.ToString("yyyy-MM-dd");
        }
        catch (Exception ex)
        {
            //throw ex;
        }
        return reString;
    }
    #endregion

 
}
